package com.foreveross.crawl.workers;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.quartz.StatefulJob;

import com.foreveross.crawl.application.plane.IPlaneInfoApplication;
import com.foreveross.crawl.application.source.IGrapSourceDataApplication;
import com.foreveross.crawl.common.util.SpringHelper;
/**
 * 提供有状态的父类worker
 * @author Administrator
 *
 */
public abstract class AbstractTaskStatefulJobWorker implements StatefulJob  {
	
	public static Logger _log = Logger.getLogger(AbstractTaskStatefulJobWorker.class);
	
	protected IPlaneInfoApplication planeInfoService;
	
	protected IGrapSourceDataApplication grapSourceDataService;

	@SuppressWarnings("rawtypes")
	private static Map<Class,Long> LastEndTime=new HashMap<Class,Long>();
	
	/**
	 * 实例化的时候，同时实例化所有的计数器
	 */
	public AbstractTaskStatefulJobWorker(){
		planeInfoService = (IPlaneInfoApplication)SpringHelper.getBean(IPlaneInfoApplication.class);
		grapSourceDataService = (IGrapSourceDataApplication)SpringHelper.getBean(IGrapSourceDataApplication.class); 
	}
	
	/**
	 * 通知结束
	 */
	@SuppressWarnings("rawtypes")
	public final void notifyEnd(Class c){
		LastEndTime.put(c, Long.valueOf(System.currentTimeMillis()));
	}
	/**
	 * 检测两次启动间隔时间是否足够指定的间隔
	 * @param interval
	 * @return
	 */
	public final boolean validateEnoughInterval(Class c,Long interval){
		if(LastEndTime.get(c)==null || interval==null){
			return true;
		}else{
			return !(System.currentTimeMillis()-LastEndTime.get(c)<interval-1000);
		}
	}

}
